package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam param) {
        log.debug("用户登录，参数：{}", param);
        UserVO userVO = userMapper.selectByUsername(param.getUsername());
        if(userVO==null){
            log.debug("用户名或密码错误");
            throw new ServiceException(StatusCode.USERNAME_ERROR);

        }
        if (!(userVO.getPassword().equals(param.getPassword()))){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    /**
     * 添加用户
     * @param
     */
    @Override
    public void save(UserSaveParam param) {
        User user = new User();
        BeanUtils.copyProperties(param,user);
        if(user.getId()==null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            if(userMapper.selectByUsername(user.getUsername())!=null){
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            userMapper.insert(user);
        }else{
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> list(UserQuery query) {
        log.debug("查询用户，参数：{}",query);
      List<UserVO> userVO =  userMapper.list(query);
        return userVO;
    }

    @Override
    public void delete(Long userId) {
        userMapper.delete(userId);
    }

    @Override
    public void updateUser(Long userId, String status) {
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updatePassword(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public List<UserVO> select(Long userId) {
        log.debug("查询用户，参数：{}",userId);
        List<UserVO> list= new ArrayList<>();
        UserVO userVO1 = userMapper.selectById(userId);
        list.add(userVO1);
        if (userVO1.getParentId()!=null && userVO1 !=null){
            UserVO userVO2 = userMapper.selectById(userVO1.getParentId());
            list.add(userVO2);
        }
        return list;
    }
}
